/**
 * Program   :   ChareSetHelpers.java
 * Author    :   shadow guo
 * Create    :   2008-5-13 下午11:01:34
 *
 * Copyright 2008 by Tongfu Information Technology Co. Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Tongfu Information Technology Co. Ltd. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Tongfu Information Technology Co. Ltd.
 * 
 */
package com.tongfusoft.utils;

import java.io.UnsupportedEncodingException;
import java.util.Locale;

/**
 * @author shadow
 * @version 1.0 字符集常用工具
 */
public class CharsetUtils {
	/**
	 * ISO8859-1转UTF8
	 * 
	 * @param str
	 * @return
	 */
	public static String getISO2UTF(String str) {
		return convertEncoding(str, "ISO-8859-1", "UTF-8");
	}

	/**
	 * UTF转ISO8859-1
	 * @param str
	 * @return
	 */
	public static String getUTF2ISO(String str) {
		return convertEncoding(str, "UTF-8", "ISO-8859-1");
	}

	/**
	 * 转换编码方式
	 * 
	 * @param str
	 * @param old_charset
	 * @param new_charset
	 * @return
	 */
	public static String convertEncoding(String str, String old_charset,
			String new_charset) {
		try {
			return new String(str.getBytes(old_charset), new_charset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 获得系统的charset
	 * 
	 * @return
	 */
	public static String getDefaultCharset() {
		Locale locale = Locale.getDefault();
		String country = locale.getCountry();
		if (country.equals("CN")) {
			return "GBK";
		} else if (country.endsWith("TW") || country.endsWith("HK")) {
			return "BIG5";
		} else {
			return "UTF-8";
		}
	}

}
